package com.miao.elm.controller;

import java.util.Arrays;
import java.util.List;

import com.miao.elm.entity.DeliveryaddressEntity;
import com.miao.elm.service.DeliveryaddressService;
import com.miao.elm.to.DeliveryAddressTo;
import com.miao.elm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
//@RefreshScope
@RequestMapping("DeliveryAddressController")
public class DeliveryaddressController {
    @Autowired
    private DeliveryaddressService deliveryaddressService;

    @RequestMapping("/listDeliveryAddressByUserId")
    public List<DeliveryaddressEntity> listDeliveryAddressByUserId(String userId){
        return deliveryaddressService.listDeliveryAddressByUserId(userId);
    }

    @RequestMapping("/getDeliveryAddressById")
    public DeliveryaddressEntity getDeliveryAddressById(Integer daId){
        return deliveryaddressService.getDeliveryAddressById(daId);
    }

    @PostMapping("/saveDeliveryAddress")
    public int saveDeliveryAddress(DeliveryAddressTo deliveryAddressTo){
        return deliveryaddressService.saveDeliveryAddress(deliveryAddressTo);
    }

    @PostMapping("/updateDeliveryAddress")
    public int updateDeliveryAddress(DeliveryAddressTo deliveryAddressTo){
        return deliveryaddressService.updateDeliveryAddress(deliveryAddressTo);
    }

    @PostMapping("/removeDeliveryAddress")
    public int removeDeliveryAddress(Integer daId){
        return deliveryaddressService.removeDeliveryAddress(daId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{daId}")
    public R info(@PathVariable("daId") Integer daId){
		DeliveryaddressEntity deliveryaddress = deliveryaddressService.getById(daId);

        return R.ok().put("deliveryaddress", deliveryaddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DeliveryaddressEntity deliveryaddress){
		deliveryaddressService.save(deliveryaddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeliveryaddressEntity deliveryaddress){
		deliveryaddressService.updateById(deliveryaddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] daIds){
		deliveryaddressService.removeByIds(Arrays.asList(daIds));

        return R.ok();
    }

}
